<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Models\User;


class UserController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $keyword = $request->get('keyword','');
        $role_id = $request->get('role_id',0);
        $export = $request->get('export',0);
        $mode = $request->get('mode',0);
        $builder = User::with('role')->where([]);
        if(!empty($keyword)){
            $builder->where(function($q) use($keyword){
                $q->where('name','like','%'.$keyword.'%')
                  ->orWhere('loginid','like','%'.$keyword.'%');
            });
        }

        if($role_id > 0){
            $builder->where(['role_id'=>$role_id]);
        }

        $builder->orderBy('id','desc');

        if($export == 1){
            $list = $builder->get();
            //清空缓冲区
            ob_clean();
            Excel::create(date('YmdHis').'_导出结果', function($excel) use($list) {
                 $excel->sheet('Sheet1', function($sheet) use($list) {
                     $headers = ['用户名','角色','帐号','创建时间'];
                     $sheet->row(1,$headers);
                     $row = 2;
                     foreach ($list as $model) {
                         $sheet->row($row++,[
                            $model->custom_name,
                            $model->intention,
                            $model->contact_name,
                            $model->contact_mobile,
                            $model->user->name,
                            $model->create_at
                        ]);
                     }
                 });
            })->download('xls');
        }else{
            if($mode == 0){
                $pager = $this->build_pager_data($builder->paginate(10));
                return response()->json(json_success($pager));
            }
            elseif($mode == 1){
                return response()->json(json_success($builder->get()));
            }
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
